//
//  BaseViewController.m
//  ProjectExtension
//
//  Created by Apple on 2020/1/8.
//  Copyright © 2020 lidianchao. All rights reserved.
//

#import "BaseViewController.h"
#import "Reachability.h"
#import "HudView.h"
#import "Config.h"
#import "SpecialViewController.h"
#import "MJRefresh.h"
#import "UILabel+MyLabel.h"
#import "UIButton+Mybutton.h"
@interface BaseViewController ()
@property (nonatomic, strong)SpecialViewController *special;
@property (nonatomic, strong)Reachability *networkConn;;
@end

@implementation BaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
//导航返回按钮
- (void)goLeftTOView {
    
    [self.navigationController popViewControllerAnimated:YES];
}
/**
 *  检测网络状态
 */
- (void)checkNetWorkStateValue:(UITableView *)tab  {
    
    /**
     *  无网络的时候执行
     */
    Reachability *r = [Reachability reachabilityWithHostName:@"www.apple.com"];
    if (![r currentReachabilityStatus]) {
        [HudView showMsg:stringWithKey(@"Please check your network", nil) inView:nil];
        [self goSpecialViewController:tab];
    }
    [self networkStateChangeNSNotificationCenterWithTabelView:tab];
}
/**
 *  展示网络错误的界面
 */
- (void)goSpecialViewController:(UITableView *)tab {
    
    [_special showInView:tab];
    [tab setContentOffset:CGPointMake(0, 0)] ;
    tab.scrollEnabled = NO;
}
/**
 *  监听网络状态，断网重连后刷新，提高用户体验
 *
 *  @param tabelview 要刷新的表
 */
- (void)networkStateChangeNSNotificationCenterWithTabelView:(UITableView *)tabelview {
    /**
     *  监听网络状态
     *
     *  @param networkStateChange 网络状态发生变化时触发的方法
     */
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkStateChange:) name:kReachabilityChangedNotification object:nil];
    _networkConn = [Reachability reachabilityForInternetConnection];
    /**
     *  12月25日添加的
     */
    _networkConn.Object = tabelview;
    [_networkConn startNotifier];
}
/**
 *  判断是否有网
 */
- (BOOL)isNetworkStatus {
    Reachability *r = [Reachability reachabilityWithHostName:@"www.apple.com"];
    if (![r currentReachabilityStatus]) {
        return NO;
    }
    return YES;
}
/**
 *  网络状态发生变化
 */
- (void)networkStateChange:(NSNotification *)notification
{
    if ([self isNetworkEnable]) {
        //在此刷新界面
        UITableView *tab = _networkConn.Object ? _networkConn.Object : nil;
        if (tab && tab.mj_header) {
            tab.scrollEnabled = YES;
            [tab.mj_header beginRefreshing];
        }
    }
}
- (BOOL)isNetworkEnable
{
    BOOL flag = YES;
    Reachability *r = [Reachability reachabilityWithHostName:@"www.apple.com"];
    UITableView *tab = _networkConn.Object ? _networkConn.Object : nil;
    switch ([r currentReachabilityStatus]) {
        case NotReachable:{
            flag = NO;
            [self goSpecialViewController:tab];
        }
            break;
        case ReachableViaWWAN: {
            [_special removeFromSuperview:tab];
            tab.scrollEnabled = YES;
        }
            break;
        case ReachableViaWiFi: {
            [_special removeFromSuperview:tab];
            tab.scrollEnabled = YES;
        }
            break;
        default:
            break;
    }
    return flag;
}
/**
 创建无数据视图

 @param frame 位置信息
 @return 返回值
 */
- (UIView *)createNoMoreViewWithFrame:(CGRect)frame {
    
    UIView *view = [self createNoMoreViewWithFrame:frame withText:stringWithKey(@"no_more_data_text", nil)];
    
    return view;
}

- (UIView *)createNoMoreViewWithFrame:(CGRect)frame withText:(NSString *)string {
    
    UIView *view = [[UIView alloc] initWithFrame:frame];
    view.backgroundColor = [UIColor clearColor];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, DeviceWidth*0.26, DeviceWidth*0.26)];
    imageView.image = [UIImage imageNamed:@"noHistory"];
    imageView.center = CGPointMake(DeviceWidth/2, 80+(DeviceWidth*0.26+40)/2);
    if (frame.size.height<=(80+(DeviceWidth*0.26+40)/2)*2) {
        imageView.center = CGPointMake(DeviceWidth/2, frame.size.height/2);
    }
    [view addSubview:imageView];
    CGFloat leftDistance = 15;
    UILabel *creat = [UILabel createLabelWithFrame:CGRectMake(leftDistance, CGRectGetMaxY(imageView.frame)+10, DeviceWidth-leftDistance*2, 20) title:string textColor:K_9EAABD_color isCenter:YES font:12 andHaveUnderLine:NO board:NO];
    [view addSubview:creat];
    return view;
}
//添加投影
- (void)addShadowInView:(UIView *)view {
    
    //阴影的颜色
    view.layer.shadowColor = K_0C2A59_color.CGColor;
    //阴影的透明度
    view.layer.shadowOpacity = 0.04f;
    //阴影发散的程度
    view.layer.shadowRadius = 4.f;
    //阴影偏移量
    view.layer.shadowOffset = CGSizeMake(0,4);
    view.layer.masksToBounds = NO;
    
}

//添加投影
- (void)addShadowInView:(UIView *)view superView:(nonnull UIView *)superView {
    
    // 阴影 圆角和阴影无法在一个layer上共存，所以需要思路是再创建一个layer层，实现阴影，放到视图下边，
    CALayer *subLayer=[CALayer layer];
    CGRect fixframe = view.frame;
    subLayer.frame= fixframe;
    subLayer.cornerRadius=6;
    subLayer.masksToBounds=NO;
    subLayer.shadowColor = K_0C2A59_color.CGColor;//shadowColor阴影颜色
    subLayer.shadowOffset = CGSizeMake(0,0);//shadowOffset阴影偏移
    subLayer.shadowOpacity = 0.4;//阴影透明度，默认0
    subLayer.shadowRadius = 4.f;//阴影半径，默认3
    [superView.layer addSublayer:subLayer];
    [subLayer addSublayer:view.layer];
}
/**
 *  设置导航栏的左右按钮的属性
 *
 */
- (UIBarButtonItem *)setItemInfoWithTitle:(NSString *)title target:(id)target andSEL:(SEL)action {
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:target action:action];
    
    [item setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"PingFangSC-Light" size:15], NSFontAttributeName, [UIColor blackColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
    return item;
}

- (UIBarButtonItem *)setItemInfoWithImage:(UIImage *)image target:(id)target andSEL:(SEL)action {
    
    UIButton *btn = [UIButton createButtonWithTitle:nil frame:CGRectMake(0, 0, 30, 30) target:target andSEL:action font:15];
    btn.backgroundColor = [UIColor clearColor];
    [btn setImage:image forState:UIControlStateNormal];
    //针对 iOS11适配改写 的关键性代码, 就这么几行,完全搞定了
    UIView *containVew = [[UIView alloc] initWithFrame:CGRectMake(0, (kDevice_Is_iPhoneX?44:20)+7, 30, 30)];
    [containVew addSubview:btn];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:containVew];
    return item;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
